package com.yc.book.ui;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;

import java.sql.Date;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;

import com.yc.book.biz.BizException;
import com.yc.book.biz.BookBiz;
import com.yc.book.util.Utils;

import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class BookEditDlg extends Dialog {

	protected Object result = false;
	protected Shell shell;
	private Text idText;
	private Text nameText;
	private Text authorText;
	private DateTime pressdateDt;
	private Spinner priceSp;
	private TableItem bookInfo;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public BookEditDlg(Shell parent, int style) {
		super(parent, style);
		setText("图书信息编辑");
	}

	public BookEditDlg(Shell parent, int style, TableItem bookInfo) {
		this(parent, style);
		this.bookInfo = bookInfo;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.PRIMARY_MODAL);
		shell.setSize(384, 278);
		shell.setText("图书编辑");
		
		Label label = new Label(shell, SWT.NONE);
		label.setBounds(67, 28, 61, 17);
		label.setText("编号");
		
		idText = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		idText.setBounds(134, 25, 176, 23);
		
		Label label_1 = new Label(shell, SWT.NONE);
		label_1.setText("书名");
		label_1.setBounds(67, 58, 61, 17);
		
		nameText = new Text(shell, SWT.BORDER);
		nameText.setBounds(134, 55, 176, 23);
		
		Label label_1_1 = new Label(shell, SWT.NONE);
		label_1_1.setText("作者");
		label_1_1.setBounds(67, 87, 61, 17);
		
		authorText = new Text(shell, SWT.BORDER);
		authorText.setBounds(134, 84, 176, 23);
		
		Label label_1_2 = new Label(shell, SWT.NONE);
		label_1_2.setText("出版日期");
		label_1_2.setBounds(67, 121, 61, 17);
		
		pressdateDt = new DateTime(shell, SWT.BORDER);
		pressdateDt.setBounds(134, 114, 176, 24);
		
		Label label_2 = new Label(shell, SWT.NONE);
		label_2.setBounds(67, 152, 61, 17);
		label_2.setText("价格");
		
		priceSp = new Spinner(shell, SWT.BORDER);
		priceSp.setBounds(134, 149, 176, 23);
		
		Button button = new Button(shell, SWT.NONE);
		button.setBounds(134, 197, 80, 27);
		button.setText("保存");
		
		Button button_1 = new Button(shell, SWT.NONE);
		button_1.setBounds(230, 197, 80, 27);
		button_1.setText("取消");
		
		////////////////////////////////////////////////
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
			}
		});
		
		
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String id = idText.getText();
				String name = nameText.getText();
				String author = authorText.getText();
				int y = pressdateDt.getYear();
				int m = pressdateDt.getMonth();
				int d = pressdateDt.getDay();
				String date = String.format("%d-%d-%d", y,m,d);
				Date pressdate = Date.valueOf(date);
				int price = Integer.parseInt(priceSp.getText());
				// biz + dao
				try {
					biz.save(id, name, author, pressdate, price);
					Utils.showMessage(shell, "系统提示", "保存成功!");
					result = true;
					shell.close();
				} catch (BizException e1) {
					e1.printStackTrace();
					Utils.showMessage(shell, e1.getMessage());
					result = false;
				}
			}
		});

		if(bookInfo != null) {
			// 获取表格行第一个字段(id), 设置到文本框中
			idText.setText(bookInfo.getText(0));
			nameText.setText(bookInfo.getText(1));
			authorText.setText(bookInfo.getText(2));
			// 2020-10-10 00:00:00..
			int y = Integer.valueOf(bookInfo.getText(4).substring(0, 4));
			int m = Integer.valueOf(bookInfo.getText(4).substring(5, 7));
			int d = Integer.valueOf(bookInfo.getText(4).substring(8, 10));
			pressdateDt.setDate(y, m, d);
			// TODO 价格没有从表格中读取到, 未完待续
			//priceSp.setDigits(Integer.valueOf(bookInfo.getText(7)));
			// setSelection 是给数字框设值
			priceSp.setSelection(10);
		}
		
	

	}
	
	private BookBiz biz = new BookBiz();
}
